package com.woniu01;

import java.sql.*;
import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) throws Exception {
        test04();
    }

    private static void test04() throws ClassNotFoundException, SQLException {
        String ClassName = "com.mysql.cj.jdbc.Driver";
        Class.forName(ClassName);
        String url = "jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql="update student set name=?,age=?,password=? where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        Scanner sc = new Scanner(System.in);
        System.out.println("输入id");
        int id=sc.nextInt();
        System.out.println("输入名字");
        String name=sc.next();
        System.out.println("输入年龄");
        int age=sc.nextInt();
        System.out.println("输入密码");
        String password1=sc.next();
        preparedStatement.setString(1, name);
        preparedStatement.setInt(2, age);
        preparedStatement.setString(3, password1);
        preparedStatement.setInt(4, id);
        int count = preparedStatement.executeUpdate();
        System.out.println("修改成功");
        preparedStatement.close();
        connection.close();
    }

    private static void text03() throws ClassNotFoundException, SQLException {
        String ClassName = "com.mysql.cj.jdbc.Driver";
        Class.forName(ClassName);
        String url = "jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql="insert into student values(?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        Scanner sc = new Scanner(System.in);
        System.out.println("输入id");
        int id=sc.nextInt();
        System.out.println("输入名字");
        String name=sc.next();
        System.out.println("输入年龄");
        int age=sc.nextInt();
        System.out.println("输入密码");
        String password1=sc.next();
        preparedStatement.setInt(1, id);
        preparedStatement.setString(2, name);
        preparedStatement.setInt(3, age);
        preparedStatement.setString(4, password1);
        int count = preparedStatement.executeUpdate();
        System.out.println("添加成功");
        preparedStatement.close();
        connection.close();
    }

    private static void test02() throws ClassNotFoundException, SQLException {
        String ClassName = "com.mysql.cj.jdbc.Driver";
        Class.forName(ClassName);
        String url = "jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql="delete from student where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        Scanner sc = new Scanner(System.in);
        System.out.println("输入id");
        int id=sc.nextInt();
        preparedStatement.setInt(1, id);
        int count = preparedStatement.executeUpdate();
        System.out.println("删除成功");
        preparedStatement.close();
        connection.close();
    }

    private static void test01() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        //执行添加操作，准备sql语句
        System.out.println("输入姓名");
        String name = scanner.nextLine();
        System.out.println("输入密码");
        String pwd = scanner.nextLine();
        //列值是用?做占位符
        String sql="SELECT * FROM student WHERE NAME=? AND PASSWORD=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,name);
        preparedStatement.setString(2,pwd);
        ResultSet resultSet = preparedStatement.executeQuery();
        //判断是否存在下一条数据，同时取到下一条数据
        if(resultSet.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
